package com.itfallrain.controller;

import com.itfallrain.common.MD5Encode;
import com.itfallrain.common.UserAuth;
import com.itfallrain.entity.User;
import com.itfallrain.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller()
@RequestMapping("/ku")
public class UserController {
    @Autowired
    private UserService userService;
    @UserAuth(8)
    @RequestMapping("/user")
    @ResponseBody
    public Object user(int page, int limit){
        Map<String,Object> map=new HashMap();
//        管理分页功能
        map.put("code",0);
        map.put("msg","");
        map.put("count",userService.getUserCount());
        map.put("data",userService.getAll(page,limit));
        return map;
    }

//    新增用户
    @RequestMapping("/adds")
    @ResponseBody
    public Object adds(User user){
        Map<String,Object> map=new HashMap<String, Object>();
//        User user1=new User();
//        user1.setPassword(MD5Encode.encode(password));
        if(userService.selectAlls(user)==1){
            map.put("code",1);
            map.put("msg","新增成功！");

        }else {
            map.put("code",0);
            map.put("msg","新增失败");
        }
        return map;
    }
//    删除用户
    @RequestMapping("/del")
    @ResponseBody
    public Object dels(int id){
        Map<String,Object> map=new HashMap<String, Object>();
        int i=userService.delectAlls(id);
        if(i==1){
            map.put("code",1);
            map.put("msg","删除成功！");
        }else {
            map.put("code",0);
            map.put("msg","修改失败！");
        }
        return map;

    }
//    修改用户信息
    @RequestMapping("/edit")
    @ResponseBody
    public Object edits(User user){
        Map<String,Object> map=new HashMap<String, Object>();
        if(userService.updateAlls(user)==1){
            map.put("code",1);
            map.put("msg","修改成功");
        }else {
            map.put("code",0);
            map.put("msg","修改失败");
        }
        return map;
    }

}
